package tt.dz.biz.service.financial;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.financial.MerchantDAO;
import tt.dz.common.BusinessException;
import tt.dz.common.DZStringUtil;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.MerchantVO;

@Service("merchantService")
public class MerchantService
{
  @Resource(name="merchantDAO")
  MerchantDAO merchantDAO;
  
  public List<MerchantVO> queryIncrementMerchant(String startTime, String endTime, String providerType)
  {
    List<MerchantVO> merchantList = null;
    try
    {
      merchantList = this.merchantDAO.queryIncrementMerchant(startTime, endTime, providerType);
    }
    catch (Exception e)
    {
      LogUtil.error("查询增量运营商异常--入参：" + startTime + "--" + endTime, e);
      merchantList = new ArrayList();
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return merchantList;
  }
  
  public List<MerchantVO> listAllVirtualMerchant()
  {
    List<MerchantVO> merchantList = null;
    try
    {
      merchantList = this.merchantDAO.listAllVirtualMerchant();
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有虚拟运营商异常", e);
      merchantList = new ArrayList();
    }
    return merchantList;
  }
  
  public List<MerchantVO> listAllMerchant()
  {
    List<MerchantVO> merchantList = null;
    try
    {
      merchantList = this.merchantDAO.listAllMerchant();
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有运营商异常", e);
      merchantList = new ArrayList();
    }
    return merchantList;
  }
  
  public boolean insertMerchant(MerchantVO merchantVO)
  {
    boolean excuteFlag = false;
    try
    {
      StringBuilder merchantCode = new StringBuilder("");
      SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
      merchantCode.append(merchantVO.getIsSelf() + format.format(new Date()) + 
        DZStringUtil.getFixedLengthString(3, this.merchantDAO.querySequence("merchant_seq").toString()));
      merchantVO.setMerchantCode(merchantCode.toString());
      int i = this.merchantDAO.insertMerchant(merchantVO);
      excuteFlag = i > 0;
      if (excuteFlag) {
        this.merchantDAO.insertMerchantSubject(merchantVO);
      }
    }
    catch (Exception e)
    {
      LogUtil.error("插入运营商异常--入参：" + merchantVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public boolean updateMerchant(MerchantVO merchantVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.merchantDAO.updateMerchant(merchantVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("更新运营商异常--入参：" + merchantVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public boolean isExist(String merchantCode)
    throws Exception
  {
    try
    {
      MerchantVO merchantVO = this.merchantDAO.queryMerchantByCode(merchantCode);
      if (null == merchantVO) {
        return false;
      }
      return true;
    }
    catch (Exception e)
    {
      LogUtil.error("根据code查询运营商异常，入参merchantCode：" + merchantCode, e);
      throw e;
    }
  }
  
  public MerchantVO queryMerchantByCode(String merchantCode)
    throws Exception
  {
    try
    {
      return this.merchantDAO.queryMerchantByCode(merchantCode);
    }
    catch (Exception e)
    {
      LogUtil.error("根据code查询运营商异常，入参merchantCode：" + merchantCode, e);
      throw e;
    }
  }
  
  public int getMerchantCardNoBinded(String merchantCode)
    throws BusinessException
  {
    try
    {
      return this.merchantDAO.getMerchantCardNoBinded(merchantCode);
    }
    catch (Exception e)
    {
      LogUtil.error("查询当前运营商被绑定了卡号数量异常，入参merchantCode：" + merchantCode, e);
      throw e;
    }
  }
  
  public MerchantVO getMerchantSignalCode(String signalCode)
    throws BusinessException
  {
    return this.merchantDAO.getMerchantSignalCode(signalCode);
  }
}
